/*

01_balance_table.do

Purpose: create treatment balance table 
Inputs: student-level-panel
Outputs: balance-table, balance-table-note
	
*/

global pretreatment = "female black hispanic white repeat_grade"
global markets = "resident prek4 prek3 k hs"
global risks = "app_risk risk_real_initial risk_real_final"
	
use "$int/student-level-panel", clear

* Restrict sample to 2020 RCT app eligible 
keep if rct == 1
assert treat == 0 | treat == 1

* Fix labels
label var change_school_ever "Change school"
label var change_school_final "Change school"
label var lengthen_app_ever "Lengthen app."
label var lengthen_app_final "Lengthen app."
label var new_insert_ever "Insert new school"
label var new_insert_final "Insert new school"
label var new_append_ever "Append new school"
label var new_append_final "Append new school"
label var shorten_app_ever "Shorten app."
label var shorten_app_final "Shorten app."
label var modify_ever "Change length or school"
label var modify_final "Change length or school"

//// Balance table

cap file close f 
file open f using "$tables/balance-table.tex", write replace

file write f "\begin{tabular}{l*{4}{c}}"_n

file write f "\toprule \\[-1.0em]"_n

* Column panels
file write f "& \multicolumn{1}{c}{Control} & \multicolumn{1}{c}{Treatment}"
file write f " & \multicolumn{2}{c}{Difference}  \\ "_n
file write f "\cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-5}"_n

* Columns
file write f "& Mean & Mean & $\beta$ & SE"

file write f "\\"_n
file write f "\midrule \\[-1.0em]"_n

/// Standardize the procedure of how to fill the rows for the table body
cap program drop write_rows
program define write_rows
	foreach var in `1' {
	
	
		file write f "`:var la `var''"
			
			foreach treat in 0 1 {
				
				if `treat' == 1 {
					local ref_treat = 0
				}
				
				qui su `var' if treat == `treat'
				
				file write f "& " %4.3fc (`r(mean)')
				
				if `treat' > 0 {						
					
					* Store estimates with HOMOSKEDASTIC errors for joint test
					qui reg `var' ib0.treat ///
						i.block i.prek4#i.resident i.prek3#i.resident /// Block and market fixed effects
						i.k#i.resident i.hs#i.resident ///
						if treat < 3 // Sample of applicants with initial risk below 50%
					est sto e_`var'
					
					* Rerun same regression with robust standard errors 
					
					* For MARKETS variables, excluded market fixed effects 
					if strpos("$markets", "`var'") > 0 {
						qui reg `var' ib0.treat i.block if treat < 3 , r	
					}
					
					else {
						
					qui reg `var' ib0.treat ///
						i.block i.prek4#i.resident i.prek3#i.resident /// Block and market fixed effects
						i.k#i.resident i.hs#i.resident ///
						if treat < 3 , r // Sample of applicants with initial risk below 50%
					}
					
					/// Write coeff, se and pval in row
					* Coefficient of Treatment
					file write f " & " %4.3fc (_b[`treat'.treat])
					
					* SE of treatment
					file write f " & (" %4.3fc (_se[`treat'.treat]) ")"

				}
			} // treat
		
		file write f "\\"_n
		
	} // var

end

/// Demographics
file write f "\emph{I. Demographics} \\"
write_rows "$pretreatment"
file write f "\\ \\[-1.0em]"_n

// Risk 
file write f "\emph{II. Risk} \\"
write_rows "$risks"
file write f "\\ \\[-1.0em]"_n

/// Sim usage outcomes
file write f "\emph{III. Interaction with simulator} \\"_n
write_rows "$outcomes_sim_use"
file write f "\\ \\[-1.0em]"_n

/// App usage outcomes
file write f "\emph{IV. Choice outcomes} \\"_n
write_rows "$outcomes_sec_stage placed_any"
file write f " \\[-1.0em]"_n

file write f "\hline \\[-1.0em]"_n
file write f "\emph{N}  "

foreach treat in 0 1 {
	
	qui count if treat == `treat'
	
	file write f "&" %4.0fc (`r(N)') 
	
} // treat

forv k = 1/2 {
	
	qui count if treat == 0 | treat == 1
	file write f "&" %4.0fc (`r(N)') 
}
file write f "\\"_n

file write f "\bottomrule"_n
file write f "\end{tabular}"_n
file close f

/// Table note

cap file close f 

file open f using "$notes/balance-table-note.tex", write replace

file write f "\floatfoot{\footnotesize\textit{Notes.} Statistics in this table are estimated from the sample of "
file write f "individuals in the control group and the Simulator-only (no warnings) treatment group. "
file write f "The column panels distinguish between these two subsamples."_n
file write f " The reported coefficients $\beta$ reflect regression estimates of the "
file write f "treatment indicator on outcomes, controlling for fixed effects of randomization time blocks "
file write f "and markets, i.e. resident status by grade.}"_n

file close f 